<template>
    <div id="account">
        <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
            <el-form :inline="true" :model="AdvData">
                <el-form-item>
                    <el-input type="number" v-model="AdvData.id" placeholder="请输入要搜索的id"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="AdvData.title" placeholder="请输入要搜索的内容"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="PostAccount">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--table-->
        <el-table :data="AdvIty" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column label="标题">
                <template slot-scope="scope">{{scope.row.title}}</template>
            </el-table-column>
            <el-table-column label="URL地址">
                <template slot-scope="scope">{{scope.row.url}}</template>
            </el-table-column>
            <el-table-column label="描述">
                <template slot-scope="scope">{{scope.row.description}}</template>
            </el-table-column>
			<el-table-column label="开关" width="94">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.isShow" :on-value="1" :off-value="0"
                               @change="tableStatus(scope.row)"></el-switch>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <!-- <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>-->
                </template>
            </el-table-column>
        </el-table>

        <!--分页-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="size" :total="total" style="float:right;"></el-pagination>
        </el-col>

        <!--新增-->
        <el-dialog title="添加消息" v-model="addFormVisible" :close-on-click-modal="false" top="5%" >
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="addForm.title" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="URL地址" prop="url">
                    <el-input v-model="addForm.url" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input type="textarea" v-model="addForm.description" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="排序值" prop="sort">
                    <el-input type="number" v-model.number="addForm.sort" auto-complete="off"></el-input>
                </el-form-item>
                <!--<el-form-item label="是否放置首页">
                    <el-switch v-model="addForm.home" on-text="" :on-value=1 :off-value=0 off-text=""></el-switch>
                </el-form-item>-->
                <el-form-item label="是否显示">
                    <el-switch v-model="addForm.isShow" on-text="" :on-value=1 :off-value=0 off-text=""></el-switch>
                </el-form-item>
                <el-form-item label="图片">
                    <el-upload class="avatar-uploader" :action="$store.state.imagesUrl + 'api/common/file/upload/image'"
                               :show-file-list="false" :on-success="handleAvatarSuccess"
                               :before-upload="beforeAvatarUpload" :headers="MyHeaders">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit('addForm')" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
        <!--编辑-->
        <el-dialog title="编辑消息" v-model="editFormVisible" :close-on-click-modal="false" :before-close="abolish" top="5%">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="editForm.title" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="URL地址" prop="url">
                    <el-input v-model="editForm.url" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input type="textarea" v-model="editForm.description" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="排序值" prop="sort">
                    <el-input type="number" v-model.number="editForm.sort" auto-complete="off"></el-input>
                </el-form-item>
                <!--<el-form-item label="是否放置首页">
                    <el-switch v-model="editForm.home" on-text="" :on-value=1 :off-value=0 off-text=""></el-switch>
                </el-form-item>-->
                <el-form-item label="是否显示">
                    <el-switch v-model="editForm.isShow" on-text="" :on-value=1 :off-value=0 off-text=""></el-switch>
                </el-form-item>
                <el-form-item label="图片">
                    <el-upload class="avatar-uploader" :action="$store.state.imagesUrl + 'api/common/file/upload/image'"
                               :show-file-list="false" :on-success="handleAvatarSuccess1"
                               :before-upload="beforeAvatarUpload" :headers="MyHeaders">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="abolish">取消</el-button>
                <el-button type="primary" @click.native="editSubmit('editForm')" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import util from '../../common/js/util';
    import {securityOpenAdd,securityOpenDlete,securityOpenList,securityOpenUpdate,securityOpenListId} from '../../api/api';
    export default {
        data() {
            return {
                AdvIty: [],
                total: 0,
                page: 1,
                size: 15,
                AdvData: {
                    id: null,
                    title: ''
                },
                listLoading: false,
                /*新增*/
                addFormVisible: false,
                addLoading: false,
                addFormRules: {
                    title: {required: true, message: '请输入标题', trigger: 'blur'},
                    url: {required: true, message: '请输入URL地址', trigger: 'blur'},
                    description: {required: true, message: '请输入描述', trigger: 'blur'}
                },
                MyHeaders: {'X-Authentication': JSON.parse(sessionStorage.getItem('user'))},
                imageUrl: '',
                addForm: {
                    title: '',
                    url: '',
                    description: '',
                    sort: 0,
                    home: 0,
                    isShow: 0,
                    image: {
                        path: ''
                    },
                },
                editFormVisible: false,//编辑界面是否显示
                editFormRules:{
                    title: {required: true, message: '请输入标题', trigger: 'blur'},
                    url: {required: true, message: '请输入URL地址', trigger: 'blur'},
                    description: {required: true, message: '请输入描述', trigger: 'blur'}
                },
                editLoading: false,
                //编辑界面数据
                editForm: {
                    title: '',
                    url: '',
                    description: '',
                    sort: 0,
                    home: 0,
                    isShow: 0,
                    image: {
                        path: ''
                    }
                },
            }
        },
        methods: {
            selsChange: function (sels) {
                this.sels = sels;
            },
            handleCurrentChange(val) {
                this.page = val;
                this.PostAccount();
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = file.response.data.pathFull;
                this.addForm.image.path = file.response.data.path;
            },
            handleAvatarSuccess1(res, file) {
                this.imageUrl = file.response.data.pathFull;
                this.editForm.image.path = file.response.data.path;
            },
            abolish(){this.editFormVisible = false;this.PostAccount();},
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg' || 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG/png 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            PostAccount(){
                let para = {
                    page: this.page,
                    size: this.size,
                    data: this.AdvData
                };
                this.listLoading = true;
                securityOpenList(para).then((res) => {
                    console.log(res)
                    this.listLoading = false;
                    let {code, data} = res;
                    if (code !== 20000) {
                        this.$message({message: res.message, type: 'error'});
                    } else {
                        this.AdvIty = data.content;
                        this.total = data.totalElements;
                    }
                });
            },
            handleAdd(){
                this.addFormVisible = true;
                this.imageUrl = '';
                this.addForm = {
                    title: '',
                    url: '',
                    description: '',
                    sort: 0,
                    home: 0,
                    isShow: 0,
                    image: {
                        path: ''
                    },
                };
            },
            addSubmit(a){
                this.$refs[a].validate((valid) => {
                    if (valid) {
                        if(this.imageUrl === ''){this.$message({message: '请上传图片', type: 'error'});return;}
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = this.addForm;
                            securityOpenAdd(para).then((res) => {
                                this.addLoading = false;
                                this.$message({
                                    message: res.message,
                                    type: 'success'
                                });
                                this.addFormVisible = false;
                                this.PostAccount();
                            });
                        });
                    }
                })
            },
            handleDel(index,row){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = {id: row.id};
                    let id = row.id;
                    securityOpenDlete(id, para).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: res.message,
                            type: 'success'
                        });
                        this.PostAccount();
                    });
                }).catch(() => {
                });
            },
        	/*开关*/
	        tableStatus(row) {
	            this.loading = true;
	            securityOpenUpdate(row).then((res) => {
	                this.loading = false;
	                this.$message({message: res.message, type: 'success'});
	                this.PostAccount();
	            })
	        },
            handleEdit(index,row){
                let para = {id: row.id};
                let id = row.id;
                securityOpenListId(id, para).then((res) => {
                    if(res.code !== 20000){
                        this.$message({message: res.message, type: 'error'});
                    }else{
                        this.editForm = res.data;
                        this.editFormVisible = true;
                        this.imageUrl = res.data.image.path;
                    }
                });
            },
            editSubmit(a){
                this.$refs[a].validate((valid) => {
                    if (valid) {
                        if(this.imageUrl === ''){this.$message({message: '请上传图片', type: 'error'});return;}
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = this.editForm;
                            securityOpenUpdate(para).then((res) => {
                                this.editLoading = false;
                                this.$message({
                                    message: res.message,
                                    type: 'success'
                                });
                                this.editFormVisible = false;
                                this.PostAccount();
                            });
                        });
                    }
                })
            }
        },
        mounted(){
            this.PostAccount();
        },
    }
</script>
<style type="text/scss" lang="scss" scoped>
    #account{
        .image {
            width: auto;
            height: 20px;
            vertical-align: middle;
        }
        .avatar-uploader {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            width: 182px;
            &:hover {
                border-color: #20a0ff;
            }
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    }
</style>